package com.sudo.service.admin.service.impl;

import com.sudo.dao.admin.entity.SysMyStsDay;
import com.sudo.dao.admin.entity.SysUserBehavior;
import com.sudo.dao.admin.entity.SysUserLoginRecord;
import com.sudo.dao.admin.mapper.SysMyStsDayMapper;
import com.sudo.dao.admin.mapper.SysUserBehaviorMapper;
import com.sudo.dao.admin.mapper.SysUserLoginRecordMapper;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysMyStsDayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysMyStsDayServiceImpl extends AdminBaseService implements SysMyStsDayService {

    @Resource
    private SysUserBehaviorMapper sysUserBehaviorMapper;

    @Resource
    private SysUserLoginRecordMapper sysUserLoginRecordMapper;

    @Resource
    private SysMyStsDayMapper sysMyStsDayMapper;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void stsMyBehaviorByDay(LocalDate localDate,String username) {

        Example deleteExample = new Example(SysMyStsDay.class);
        Example.Criteria criteria = deleteExample.createCriteria();
        criteria.andEqualTo("date", localDate);
        criteria.andEqualTo("username", username);
        sysMyStsDayMapper.deleteByExample(deleteExample);

        String date2Str = localDate.toString();
        SysMyStsDay sysMyStsDay = new SysMyStsDay();
        // 某天的活跃度（操作数）
        Example myBehaviorExample = new Example(SysUserBehavior.class);
        Example.Criteria myBehaviorCriteria = myBehaviorExample.createCriteria();
        myBehaviorCriteria.andLike("operTime", date2Str+"%");
        myBehaviorCriteria.andEqualTo("username", username);
        int numBehavior = sysUserBehaviorMapper.selectCountByExample(myBehaviorExample);
        sysMyStsDay.setNumBehavior(numBehavior);

        // 某天的登录人次
        Example myLoginNotExample = new Example(SysUserLoginRecord.class);
        Example.Criteria myLoginNotCriteria = myLoginNotExample.createCriteria();
        myLoginNotCriteria.andLike("loginTime",date2Str+"%");
        myLoginNotCriteria.andEqualTo("username", username);
        int loginNot = sysUserLoginRecordMapper.selectCountByExample(myLoginNotExample);
        sysMyStsDay.setLoginNot(loginNot);

        LocalDateTime createTime = LocalDateTime.now();
        sysMyStsDay.setCreateTime(createTime);
        sysMyStsDay.setDate(localDate);
        sysMyStsDay.setUsername(username);

        sysMyStsDayMapper.insertSelective(sysMyStsDay);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public SysMyStsDay getMyStsDay(LocalDate localDate,String username) {
        Example example = new Example(SysMyStsDay.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("date",localDate);
        criteria.andEqualTo("username", username);
        SysMyStsDay sysMyStsDay = sysMyStsDayMapper.selectOneByExample(example);
        if (sysMyStsDay == null) {
            sysMyStsDay = new SysMyStsDay(localDate);
        }
        return sysMyStsDay;
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public List<SysMyStsDay> getSysMyStsDayList(LocalDate localDate,String username){
        LocalDate localDate1 = localDate.minusDays(30);
        Example example = new Example(SysMyStsDay.class);
        example.orderBy("date").asc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andLessThanOrEqualTo("date",localDate);
        criteria.andGreaterThanOrEqualTo("date",localDate1);
        return sysMyStsDayMapper.selectByExample(example);
    }

}
